package com.tsp_tux.demo.Service;

import com.alibaba.fastjson.JSONObject;
import com.tsp_tux.demo.Dao.UserRepository;
import com.tsp_tux.demo.Entity.RespBean;
import com.tsp_tux.demo.Entity.User;
import com.tsp_tux.demo.Util.Login;
import com.tsp_tux.demo.Util.Lt;
import com.tsp_tux.demo.Util.easyRespBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Service
public class LoginServiceImp implements LoginService {

    @Autowired
    UserRepository userRepository;


    @Override
    public RespBean studentLogin(String uid, String rsa, int ul, int pl) throws IOException {
        String result = Login.studentLogin(rsa, ul, pl);

        //说明登录失败
        if (result == null) {
            return easyRespBean.err();
        }

        //登录成功
        //先判断有没有成功登录过
        User user = userRepository.findByUid(uid);
        if (user == null) {
            User user1 = new User();
            user1.setUid(uid);
            user1.setCategory(0);
            userRepository.save(user1);
        }

        JSONObject jsonObject = JSONObject.parseObject(result).getJSONObject("object");

        return easyRespBean.ok(jsonObject);
    }

    @Override
    public RespBean teacherLogin(String uid, String rsa, int ul, int pl) throws IOException {
        String result = Login.teacherLogin(uid, rsa, ul, pl);

        //说明登录失败
        if (result == null) {
            return easyRespBean.err();
        }

        //登录成功
        //先判断有没有成功登录过
        User user = userRepository.findByUid(uid);
        if (user == null) {
            User user1 = new User();
            user1.setUid(uid);
            user1.setCategory(1);
            userRepository.save(user1);
        }

        JSONObject jsonObject = JSONObject.parseObject(result).getJSONObject("object");

        return easyRespBean.ok(jsonObject);
    }

    @Override
    public RespBean login(String uid, String rsa, String lt, String cookies, int ul, int pl) throws IOException {
        Lt aLt = new Lt("https://pass.sdu.edu.cn/cas/login?service=http%3A%2F%2Fbkjws.sdu.edu.cn%2Ff%2Fj_spring_security_thauth_roaming_entry");
        aLt.setLt(lt);
        aLt.setCookies(cookies);

        String result = Login.login(aLt, uid, rsa, ul, pl);

        //说明登录失败
        if (result == null) {
            return easyRespBean.err();
        }

        //把返回的信息数据转成json
        JSONObject jsonObject = JSONObject.parseObject(result).getJSONObject("object");

        //登录成功
        //先判断有没有成功登录过
        User user = userRepository.findByUid(uid);
        if (user == null) {
            User tempUser = new User();
            tempUser.setUid(uid);
            if (jsonObject.get("jsh") == null) {
                //说明是学生
                tempUser.setCategory(0);
                tempUser.setUsername((String) jsonObject.get("xm"));
                tempUser.setIconurl("http://qvg990rii.hb-bkt.clouddn.com/akari.jpg");
            }else{
                //否则是教师
                tempUser.setCategory(1);
                tempUser.setUsername((String) jsonObject.get("jsm"));
                tempUser.setIconurl("http://qvg990rii.hb-bkt.clouddn.com/akari.jpg");
            }
            userRepository.save(tempUser);

            return easyRespBean.ok(tempUser);
        }

        return easyRespBean.ok(user);

    }

    @Override
    public RespBean getLt() throws IOException {
        Lt lt = new Lt("https://pass.sdu.edu.cn/cas/login?service=http%3A%2F%2Fbkjws.sdu.edu.cn%2Ff%2Fj_spring_security_thauth_roaming_entry");
        lt.obtain();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("lt", lt.getLt());
        jsonObject.put("cookies", lt.getCookies());
        return easyRespBean.ok(jsonObject);
    }
}
